package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.OrganizationPO;
import com.ajmd.onlineexam.entity.QuestionBankPO;
import com.ajmd.onlineexam.entity.QuestionPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.OrganizationService;
import com.ajmd.onlineexam.vo.OrganizationVO;
import com.ajmd.onlineexam.vo.QuestionVO;
import com.ajmd.onlineexam.vo.SelectOptionVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ajmd
 * @since 2023/5/1 17:07
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController {
    @Resource
    private OrganizationService organizationService;

    @PostMapping("/addOrUpdate")
    public CommonResult addOrUpdate(@RequestBody OrganizationPO organizationPO){
        if(ObjectUtils.isEmpty(organizationPO)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        int num = organizationService.addOrUpdate(organizationPO);
        Boolean isEdit =ObjectUtils.isNotEmpty(organizationPO.getId());
        if(num==0){
            if(isEdit){
                return CommonResult.error().message("编辑组织失败！");
            }
            return CommonResult.error().message("新建组织失败！");
        }
        if(isEdit){
            return CommonResult.ok().message("编辑组织成功！");
        }
        return CommonResult.ok().message("新建组织成功！");
    }

    @PostMapping("/select")
    public CommonResult getOrganizations(@MultiRequestBody Integer userType, @MultiRequestBody Integer currentPage,
                                     @MultiRequestBody Integer pageSize, @MultiRequestBody SearchParam searchParam){

        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        Page<OrganizationPO> pageData = organizationService.getPage( currentPage, pageSize, searchParam);
        return CommonResult.ok().data("pageData",pageData);
    }

    @PostMapping("selectList")
    public CommonResult getList(@MultiRequestBody Integer userType,
                                @MultiRequestBody Integer level){
        if(ObjectUtils.isEmpty(userType) || ObjectUtils.isEmpty(level)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
//        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
//            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
//        }
        List<SelectOptionVO> list = organizationService.getList(level);
        return CommonResult.ok().data("optionList",list);
    }


    @PostMapping("/selectOne")
    public CommonResult getOrganization(@RequestParam String id){
        OrganizationPO organizationPO = organizationService.getOne(id);
        return CommonResult.ok().data("organizationPO",organizationPO);
    }

    @DeleteMapping("/delete")
    public CommonResult deleteById(@RequestParam String id){
        int num = organizationService.delete(id);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.DELETE_ERR);
        }
        return CommonResult.ok().message("组织删除成功");
    }


}
